#!/usr/bin/python
#-*- coding: utf-8 -*-


from flask import Flask, url_for,render_template,request
from flask_flatpages import FlatPages
import MySQLdb as mysql
import json





DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'


app = Flask(__name__)
app.config.from_object(__name__)
flatpages = FlatPages(app)

conn = mysql.connect(host = "192.168.0.9", user = "root", passwd ="p@ssw0rd",db = "blogs",port = 3306)
conn.commit()
cur = conn.cursor()




@app.route('/', methods=["GET","POST"])
def index():
    sql =""
    if request.method=="POST":
        data = request.json
        try:
            sql = "INSERT INTO `register` (`id`,`phone`,`email`,`passwd`,`en_passwd`) VALUES('%s', '%s', '%s', '%s', '%s')" % (data['id'],data['phone'], data['email'], data['passwd'], data['en_passwd'])
            ret = cur.execute(sql)
        except mysql.IntegrityError:
            pass
        return "OK"
    else:
        pages = (p for p in flatpages if 'date' in p.meta)
        return  render_template('index.html', pages=pages)

@app.route("/data", methods=["GET"])
def getdata():
     cur.execute("SELECT `passwd`,`en_passwd` FROM `register`")
     ones = [[i[0]*1000, i[1]] for i in cur.fetchall()]
     return "%s(%s);" % (request.args.get('callback'), json.dumps(ones))

@app.route('/')
def blogs():
    pages = (p for p in flatpages if 'date' in p.meta)
    return  render_template('blogs.html', pages=pages)


@app.route('/pages/<path:path>/')
def page(path):
    page = flatpages.get_or_404(path)
    return render_template('page.html', page=page)


if __name__ == '__main__':
    app.run(host='192.168.0.9',port=9000)